package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.entity.ScanWirelessControllerBean;
import com.qiantu.phone.R;
import com.qiantu.phone.glide.GlideApp;
import com.qiantu.phone.ui.adapter.SelectGatewayAdapter;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.KeyboardUtils;

import java.util.List;

public class BottomAddDeviceEditDialog extends BottomPopupView {

    private DialogClickListener mDialogClickListener;
    private View deviceNameLayout;
    private TextView nameTextview;
    private EditText mEditDeviceName;
    private TextView iconTextview;
    private View btnDeviceIcon;
    private ImageView iconDevice;
    private View btnFloorRoom;
    private TextView tvFloorRoom;
    private TextView selectGatewayTitle;
    private RecyclerView gatewayRecyclerView;
    private TextView btnSave;
    private String deviceName;
    private String hint;
    private String floorRoom;
    private String btnText;
    private String deviceIcon;
    private int editType;

    private int deviceIconRes;
    private int maxLength;
    //0 添加设备 1添加控制器
    private int addDeviceType;
    private SelectGatewayAdapter mSelectGatewayAdapter;
    private List<ScanWirelessControllerBean.MasterController> mGatewayData;

    public BottomAddDeviceEditDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_add_device_edit;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        nameTextview = findViewById(R.id.edit_device_name_title);
        iconTextview = findViewById(R.id.icon_device_title);
        deviceNameLayout = findViewById(R.id.device_name_layout);
        mEditDeviceName = findViewById(R.id.edit_device_name);
        btnDeviceIcon = findViewById(R.id.btn_device_icon);
        iconDevice = findViewById(R.id.icon_device);
        btnFloorRoom = findViewById(R.id.btn_floor_room);
        tvFloorRoom = findViewById(R.id.tv_floor_room);
        selectGatewayTitle = findViewById(R.id.select_gateway_title);
        gatewayRecyclerView = findViewById(R.id.gateway_recycler_view);
        btnSave = findViewById(R.id.btn_save);
        btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mDialogClickListener != null){
                    mDialogClickListener.onConfirmClick();
                }
            }
        });

        btnDeviceIcon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mDialogClickListener != null){
                    mDialogClickListener.onDeviceIconClick();
                }
            }
        });

        btnFloorRoom.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mDialogClickListener != null){
                    mDialogClickListener.onDeviceFloorRoomClick();
                }
            }
        });
        //添加控制器，只能设置名称
        if(addDeviceType == 1){
            btnDeviceIcon.setVisibility(GONE);
            btnFloorRoom.setVisibility(GONE);
        }else{
            btnDeviceIcon.setVisibility(VISIBLE);
            btnFloorRoom.setVisibility(VISIBLE);
        }
        mEditDeviceName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().isEmpty() || (btnFloorRoom.getVisibility() == VISIBLE && TextUtils.isEmpty(floorRoom))){
                    btnSave.setEnabled(false);
                }else {
                    btnSave.setEnabled(true);
                }
            }
        });
        if(!TextUtils.isEmpty(floorRoom)){
            tvFloorRoom.setText(floorRoom);
        }
        if(!TextUtils.isEmpty(deviceIcon)){
            GlideApp.with(iconDevice).load(deviceIcon).into(iconDevice);
        }
        if(deviceIconRes != 0){
            iconDevice.setImageResource(deviceIconRes);
        }
        if(!TextUtils.isEmpty(deviceIcon)){
            GlideApp.with(iconDevice).load(deviceIcon).into(iconDevice);
        }
        if(!TextUtils.isEmpty(btnText)){
            setBtnText(btnText);
        }
        mEditDeviceName.setText(deviceName);
        if(!TextUtils.isEmpty(hint)){
            mEditDeviceName.setHint(hint);
        }
        if(maxLength != 0){
            InputFilter[] filters = { new InputFilter.LengthFilter(maxLength) };
            mEditDeviceName.setFilters(filters);
        }
        if(!TextUtils.isEmpty(deviceName)){
            mEditDeviceName.setSelection(deviceName.length());
        }
        if(mGatewayData != null){
            setSelectGatewayView();
        }
        if(editType == 2){
            nameTextview.setText(R.string.device_group_name);
            iconTextview.setText(R.string.device_group_icon);
        }else{
            nameTextview.setText(R.string.device_name);
            iconTextview.setText(R.string.device_icon);
        }
    }

    public void setBtnText(String btnText){
        this.btnText = btnText;
        if(btnSave != null){
            btnSave.setText(btnText);
        }
    }

    public void setFirstStep(){
        if(deviceNameLayout != null){
            deviceNameLayout.setVisibility(VISIBLE);
            mEditDeviceName.setVisibility(VISIBLE);
            btnDeviceIcon.setVisibility(VISIBLE);
            btnFloorRoom.setVisibility(VISIBLE);
            selectGatewayTitle.setVisibility(GONE);
            gatewayRecyclerView.setVisibility(GONE);
        }
    }

    public String getBtnText(){
        if(btnSave != null){
            return btnSave.getText().toString();
        }
        return null;
    }

    public void setFloorRoom(String text){
        this.floorRoom = text;
        if(tvFloorRoom != null){
            tvFloorRoom.setText(text);
            if (TextUtils.isEmpty(mEditDeviceName.getText()) || (TextUtils.isEmpty(floorRoom))){
                btnSave.setEnabled(false);
            }else {
                btnSave.setEnabled(true);
            }
        }
    }

    public void setDeviceIcon(String icon){
        this.deviceIcon = icon;
        if(iconDevice != null){
            GlideApp.with(iconDevice).load(icon).into(iconDevice);
        }
    }

    public void setDeviceIcon(int iconRes){
        this.deviceIconRes = iconRes;
        if(iconDevice != null){
            iconDevice.setImageResource(deviceIconRes);
        }
    }

    public void setAddDeviceType(int addDeviceType){
        this.addDeviceType = addDeviceType;
        //添加控制器，只能设置名称
        if(btnDeviceIcon != null){
            if(addDeviceType == 1){
                btnDeviceIcon.setVisibility(GONE);
                btnFloorRoom.setVisibility(GONE);
            }else{
                btnDeviceIcon.setVisibility(VISIBLE);
                btnFloorRoom.setVisibility(VISIBLE);
            }
        }
    }

    public void setEditType(int editType){
//      1为普通，2为组
        this.editType = editType;
    }

    public void showSelectGateway(List<ScanWirelessControllerBean.MasterController> gatewayData){
        this.mGatewayData = gatewayData;
        if(gatewayRecyclerView != null){
            setSelectGatewayView();
        }
    }

    private void setSelectGatewayView(){
        deviceNameLayout.setVisibility(GONE);
        mEditDeviceName.setVisibility(GONE);
        btnDeviceIcon.setVisibility(GONE);
        btnFloorRoom.setVisibility(GONE);
        selectGatewayTitle.setVisibility(VISIBLE);
        gatewayRecyclerView.setVisibility(VISIBLE);
        setBtnText(getResources().getString(R.string.finish_add));
        btnSave.setEnabled(true);
        if(mSelectGatewayAdapter == null){
            mSelectGatewayAdapter = new SelectGatewayAdapter(getContext());
            gatewayRecyclerView.setAdapter(mSelectGatewayAdapter);
            gatewayRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        }
        mSelectGatewayAdapter.setSelected(mGatewayData.get(0));
        mSelectGatewayAdapter.setData(mGatewayData);
    }

    public ScanWirelessControllerBean.MasterController getSelectedGateway(){
        if(mSelectGatewayAdapter != null){
            return mSelectGatewayAdapter.getSelectedData();
        }
        return null;
    }

    public void setMaxLength(int maxLength){
        this.maxLength = maxLength;
        if(mEditDeviceName != null){
            InputFilter[] filters = { new InputFilter.LengthFilter(maxLength) };
            mEditDeviceName.setFilters(filters);
        }
    }

    public void setDeviceName(String text){
        this.deviceName = text;
        if(mEditDeviceName != null){
            mEditDeviceName.setText(text);
            if(!TextUtils.isEmpty(text)){
                mEditDeviceName.setSelection(text.length());
            }
        }
    }

    public void setHint(String hint){
        this.hint = hint;
        if(mEditDeviceName != null){
            mEditDeviceName.setHint(hint);
        }
    }

    public String getText() {
        if(mEditDeviceName != null){
            deviceName = mEditDeviceName.getText().toString();
        }
        return deviceName;
    }
    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    public void setDialogClickListener(@Nullable DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void beforeShow() {
        super.beforeShow();
        if(mEditDeviceName.getVisibility() == VISIBLE){
            showSoftInput(mEditDeviceName);
        }
    }

    public interface DialogClickListener {
        void onDeviceIconClick();
        void onDeviceFloorRoomClick();
        void onConfirmClick();
    }

    @Override
    protected void beforeDismiss() {
        super.beforeDismiss();
        KeyboardUtils.hideSoftInput(mEditDeviceName);
    }

    public void hideKeyboard(){
        KeyboardUtils.hideSoftInput(mEditDeviceName);
    }
}
